<?php

namespace App\Admin\Controllers;

include base_path() . '/vendor/autoload.php';

use DB;
use Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\Broadcast;
use App\BroadcastLog;

class BroadcastController extends Controller
{
	/**
	 * [index 通知列表]
	 * @return [type] [description]
	 */
	public function index(Request $request)
	{
		$keywords = $request->input('keywords');
		$casts = Broadcast::where(['status' => 1]);
		if ($keywords) {	
			$casts = $casts->where(function($query) use ($keywords){
				$query->where('title', 'like', '%'.$keywords.'%')->orWhere('content', 'like', '%'.$keywords.'%');
			});
		}
		$casts = $casts->orderBy('created_at', 'DESC')->paginate(15);
		$casts->appends([
			'keywords' => $keywords
			]);
		return view('admin.broadcast.index', compact('keywords', 'casts'));
	}

	/**
	 * [create 新增通知]
	 * @return [type] [description]
	 */
	public function create()
	{
		return view('admin.broadcast.create');
	}

	/**
	 * [store 新增通知]
	 * @return [type] [description]
	 */
	public function store(Request $request)
	{
    	$validator = Validator::make($request->all(), [
			'content' => 'required',
            'title' => 'required'
        ], [
        	'content.required' => '请设置通知内容',
        	'title.required' => '请设置通知标题'
        ]);

        if ($validator->passes()) {
        	$cast = new Broadcast;
        	$cast->title = $request->input('title');
        	$cast->content = $request->input('content');
        	$cast->save();

            return ['flag' => true, 'msg' => '创建成功'];
        } else {
            return $validator->errors();
        }
	}

	/**
	 * [edit 编辑通知]
	 * @return [type] [description]
	 */
	public function edit(Broadcast $cast)
	{
		return view('admin.broadcast.edit', compact('cast'));
	}

	/**
	 * [restore 编辑通知]
	 * @return [type] [description]
	 */
	public function restore(Broadcast $cast, Request $request)
	{
    	$validator = Validator::make($request->all(), [
			'content' => 'required',
            'title' => 'required'
        ], [
        	'content.required' => '请设置通知内容',
        	'title.required' => '请设置通知标题'
        ]);

        if ($validator->passes()) {
        	$cast->title = $request->input('title');
        	$cast->content = $request->input('content');
        	$cast->save();

            return ['flag' => true, 'msg' => '编辑成功'];
        } else {
            return $validator->errors();
        }
	}

	/**
	 * [delete description]
	 * @return [type] [description]
	 */
	public function delete(Broadcast $cast)
	{
		return view('admin.broadcast.delete', compact('cast'));
	}

	/**
	 * [remove description]
	 * @param  Broadcast $cast [description]
	 * @return [type]          [description]
	 */
	public function remove(Broadcast $cast)
	{
		$cast->status = 0;
		$cast->save();
		return ['flag' => true, 'msg' => '删除成功'];
	}

	/**
	 * [voice description]
	 * @param  Broadcast $cast [description]
	 * @return [type]          [description]
	 */
	public function voice(Broadcast $cast)
	{
		return view('admin.broadcast.voice', compact('cast'));
	}

	/**
	 * [doVoice description]
	 * @return [type] [description]
	 */
	public function doVoice(Broadcast $cast)
	{
		$url = 'http://39.106.36.158:9001/trumpetOfgame?content='.urlencode($cast->content);
		$curl = curl_init();
		//设置抓取的url
		curl_setopt($curl, CURLOPT_URL, $url);
		//设置获取的信息以文件流的形式返回，而不是直接输出。
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		//执行命令
		$data = curl_exec($curl);
		//关闭URL请求
		curl_close($curl);
		//显示获得的数据
		$data = json_decode($data, true);
		if ($data['errcode'] == 0) {
			$log = new BroadcastLog;
			$log->bid = $cast->id;	
			$log->save();

			return ['flag' => true, 'msg' => '发送成功'];
		} else {
			return ['flag' => true, 'msg' => $data['errmsg']];
		}
	}

	/**
	 * [log description]
	 * @return [type] [description]
	 */
	public function log(Broadcast $cast)
	{
		$logs = BroadcastLog::where(['bid' => $cast->id])->orderBy('created_at', 'DESC')->paginate(10);
		return view('admin.broadcast.log', compact('logs', 'cast'));
	}

	/**
	 * [log_page description]
	 * @return [type] [description]
	 */
	public function logPage(Request $request, Broadcast $cast)
	{
		$logs = BroadcastLog::where(['bid' => $cast->id])->where('id', '<', $request->input('offset'))->limit(10)->orderBy('created_at', 'DESC')->get()->toArray();
		if ($logs) {
			foreach ($logs as $key => $log) {
				$logs[$key]['id'] = $log['id'];
				$logs[$key]['title'] = $cast->title;
				$logs[$key]['content'] = mb_substr($cast->content, 0, 10) . '...';
				$logs[$key]['created_at'] = substr($log['created_at'], 0, 16);
			}
			return [
				'flag' => true,
				'data' => $logs
			];
		} else {
			return [
				'flag' => false
			];
		}
	}

}	